import { IsNumber, IsOptional, IsString, MinLength } from 'class-validator';
import { applyDecorators } from '@nestjs/common';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

interface FileFieldOptions {
  required?: boolean;
  min?: number;
  description?: string;
  default?: any;
}

function FileField(
  decorators: PropertyDecorator[],
  options?: FileFieldOptions,
) {
  if (!options?.required) {
    decorators.push(IsOptional());
    decorators.push(
      ApiPropertyOptional({
        description: options?.description,
        default: options?.default || null,
      }),
    );
  } else {
    decorators.push(
      ApiProperty({
        description: options?.description,
        default: options?.default || null,
      }),
    );
  }
  return applyDecorators(...decorators);
}

export function StringField(options?: FileFieldOptions) {
  const decorators = [IsString()];
  FileField(decorators, options);
  if (options?.min) decorators.push(MinLength(options.min));
  return applyDecorators(...decorators);
}

export function NumberField(options?: FileFieldOptions) {
  const decorators = [IsNumber()];
  FileField(decorators, options);
  if (options?.min) decorators.push(MinLength(options.min));
  return applyDecorators(...decorators);
}
